package com.loveprogrammer.eternity.factory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.loveprogrammer.eternity.model.mission.*;
import com.loveprogrammer.eternity.skill.support.skills.AbstractSkill;
import lombok.Data;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * @ClassName MissionFactory
 * @Description 任务工厂
 * @Author admin
 * @Date 2023/8/22 19:10
 * @Version 1.0
 */
@Data
public class MissionFactory {

    // 待生成的对象
    String className = null;
    String rewardClassName = null;

    MissionDefinition source = null;

    // 入参
    String paramJson = "";
    String rewardParams = "";
    String properties = "";

    Integer taskTargetType = 0;
    MissionReward reward = null;
    int goldReword = 0;
    int diamondsReword = 0;
    int exp = 0;

    public MissionFactory MissionBuilder(){
        return this;
    }

    public MissionFactory className(String className){
        this.className = className;
        return this;
    }

    public MissionFactory params(String json){
        this.paramJson = json;
        return this;
    }



    public MissionDefinition builder() throws Exception {
        // 奖励
        Class<?> rewardClazz = Class.forName(rewardClassName);
        Constructor<?>[] rewardConstructors = rewardClazz.getConstructors();
        LinkedHashMap rewardParamMap = JSON.parseObject(rewardParams, LinkedHashMap.class);
        RewardProperties rewardProperties = JSON.parseObject(properties, RewardProperties.class);

        Object[] rewardArgs = new Object[]{rewardParamMap,rewardProperties,goldReword,diamondsReword,exp};
        for (Constructor<?> rewardConstructor : rewardConstructors) {
            // 找到字段长度一致的构造函数
            if (rewardConstructor.getParameterTypes().length == rewardArgs.length) {
                reward = (MissionReward) rewardClazz.getDeclaredConstructor(rewardConstructor.getParameterTypes()).newInstance(rewardArgs);
                break;
            }
        }

        // 整个
        Class<?> clazz = Class.forName(className);
        Constructor<?>[] constructors = clazz.getConstructors();
        LinkedHashMap map = JSON.parseObject(paramJson, LinkedHashMap.class);
        map.put("taskTargetType",taskTargetType);
        map.put("reward",reward);

        Object[] args = map.values().toArray();
        int size = map.size();
        // 遍历所有的构造方法
        for (Constructor<?> constructor : constructors) {
            // 找到字段长度一致的构造方法
            if (constructor.getParameterTypes().length == size) {
                source = (MissionDefinition) clazz.getDeclaredConstructor(constructor.getParameterTypes()).newInstance(args);
                break;
            }
        }
        return source;
    }

    public MissionFactory taskTargetType(int taskTargetType) {
        this.taskTargetType = taskTargetType;
        return this;
    }

    public MissionFactory rewardClassName(String rewardClassName) {
        this.rewardClassName = rewardClassName;
        return this;
    }

    public MissionFactory rewardParams(String rewardParams) {
        this.rewardParams = rewardParams;
        return this;
    }

    public MissionFactory rewardProperties(String properties) {
        this.properties = properties;
        return this;
    }

    public MissionFactory rewardGoldCoin(int num) {
        this.goldReword = num;
        return this;
    }

    public MissionFactory rewardDiamonds(int num) {
        this.diamondsReword = num;
        return this;
    }

    public MissionFactory rewardExp(int exp) {
        this.exp = exp;
        return this;
    }
}
